﻿using Microsoft.IdentityModel.Tokens;
using SqlSugar.Extensions;
using DataAcquisitionBLL;
using DataAcquisitionDAL;
using NetTaste;

namespace DataAcquisition
{
    public partial class TemplateAddForm : BaseForm
    {
        #region 属性
        /// <summary>
        /// 模型
        /// </summary>
        private Template Services = new Template();

        #endregion

        #region 自定义方法

        /// <summary>格式化判断方式
        /// 
        /// </summary>
        /// <param name="estimate"></param>
        /// <returns></returns>
        private int FormatEstimate(string str)
        {
            if (str == "固定数值")
            {
                return 1;
            }
            else
            {
                return 2;
            }
        }
        #endregion

        #region 系统方法
        public TemplateAddForm()
        {
            InitializeComponent();
            Header.Text = "添加试压模板";
        }

        private void AddTemplateForm_Load(object sender, EventArgs e)
        {
 
        }
        #endregion

        #region 响应事件
        private void ConfirmButton_Click(object sender, EventArgs e)
        {
            TemplateModel model = new TemplateModel();
            try
            {
                model.Number = Convert.ToInt32(NumberInput.Text);
            }
            catch (Exception)
            {
                model.Number = 0;
            }
            model.PipePosition = PipePositionSelect.Text;
            model.ErrorRange = ErrorRangeInput.Text.ObjToInt();
            try
            {
                model.DesignPressure = Math.Round(double.Parse(DesignPressureInput.Text), 3);
            }
            catch (Exception)
            {
                model.DesignPressure = 0;
            }

            if (!FirstStagePressureInput.Text.IsNullOrEmpty())
            {
                try
                {
                    model.FirstStagePressure = Math.Round(double.Parse(FirstStagePressureInput.Text), 3);
                    if (!FirstStageTimeInput.Text.IsNullOrEmpty())
                    {
                        model.FirstStageTime = int.Parse(FirstStageTimeInput.Text);
                    }
                    else
                    {
                        Error("请输入阶段一持续时间");
                        return;
                    }

                    if (!FirstStageTimeUnitSelect.Text.IsNullOrEmpty())
                    {
                        model.FirstStageTimeUnit = FirstStageTimeUnitSelect.Text;
                    }
                    else
                    {
                        Error("请选择阶段一时间单位");
                        return;
                    }

                    if (!FirstStageEstimateWaySelect.Text.IsNullOrEmpty())
                    {
                        model.FirstStageEstimateWay = FormatEstimate(FirstStageEstimateWaySelect.Text);
                    }
                    else
                    {
                        Error("请选择阶段一判断方式");
                        model.FirstStageEstimateWay = 0;
                        return;
                    }

                    if (!FirstStageTimeUnitSelect.Text.IsNullOrEmpty())
                    {
                        model.FirstStageEstimateValue = Math.Round(double.Parse(FirstStageEstimateValueInput.Text), 3);
                    }
                    else
                    {
                        Error("请输入阶段一判断数值");
                        return;
                    }

                }
                catch (Exception)
                {
                    Error("请输入阶段一持续时间");
                    return;
                }
            }

            if (!SecondStagePressureInput.Text.IsNullOrEmpty())
            {
                try
                {
                    model.SecondStagePressure = Math.Round(double.Parse(SecondStagePressureInput.Text), 3);
                    if (!SecondStageTimeInput.Text.IsNullOrEmpty())
                    {
                        model.SecondStageTime = int.Parse(SecondStageTimeInput.Text);
                    }
                    else
                    {
                        Error("请输入阶段二持续时间");
                        return;
                    }

                    if (!SecondStageTimeUnitSelect.Text.IsNullOrEmpty())
                    {
                        model.SecondStageTimeUnit = SecondStageTimeUnitSelect.Text;
                    }
                    else
                    {
                        Error("请选择阶段二时间单位");
                        return;
                    }

                    if (!SecondStageEstimateWaySelect.Text.IsNullOrEmpty())
                    {
                        model.SecondStageEstimateWay = FormatEstimate(SecondStageEstimateWaySelect.Text);
                    }
                    else
                    {
                        Error("请选择阶段二判断方式");
                        model.SecondStageEstimateWay = 0;
                        return;
                    }

                    if (!SecondStageTimeUnitSelect.Text.IsNullOrEmpty())
                    {
                        model.SecondStageEstimateValue = Math.Round(double.Parse(SecondStageEstimateValueInput.Text), 3);
                    }
                    else
                    {
                        Error("请输入阶段二判断数值");
                        return;
                    }

                }
                catch (Exception)
                {
                    Error("请输入阶段二持续时间");
                    return;
                }
            }

            if (!ThirdStagePressureInput.Text.IsNullOrEmpty())
            {
                try
                {
                    model.ThirdStagePressure = Math.Round(double.Parse(ThirdStagePressureInput.Text), 3);
                    if (!ThirdStageTimeInput.Text.IsNullOrEmpty())
                    {
                        model.ThirdStageTime = int.Parse(ThirdStageTimeInput.Text);
                    }
                    else
                    {
                        Error("请输入阶段三持续时间");
                        return;
                    }

                    if (!ThirdStageTimeUnitSelect.Text.IsNullOrEmpty())
                    {
                        model.ThirdStageTimeUnit = ThirdStageTimeUnitSelect.Text;
                    }
                    else
                    {
                        Error("请选择阶段三时间单位");
                        return;
                    }

                    if (!ThirdStageEstimateWaySelect.Text.IsNullOrEmpty())
                    {
                        model.ThirdStageEstimateWay = FormatEstimate(ThirdStageEstimateWaySelect.Text);
                    }
                    else
                    {
                        Error("请选择阶段三判断方式");
                        model.ThirdStageEstimateWay = 0;
                        return;
                    }

                    if (!ThirdStageTimeUnitSelect.Text.IsNullOrEmpty())
                    {
                        model.ThirdStageEstimateValue = Math.Round(double.Parse(ThirdStageEstimateValueInput.Text), 3);
                    }
                    else
                    {
                        Error("请输入阶段三判断数值");
                        return;
                    }

                }
                catch (Exception)
                {
                    Error("请输入阶段三持续时间");
                    return;
                }
            }

            if (!IntensityStagePressureInput.Text.IsNullOrEmpty())
            {
                try
                {
                    model.IntensityStagePressure = Math.Round(double.Parse(IntensityStagePressureInput.Text), 3);
                    if (!IntensityStageTimeInput.Text.IsNullOrEmpty())
                    {
                        model.IntensityStageTime = int.Parse(IntensityStageTimeInput.Text);
                    }
                    else
                    {
                        Error("请输入强度段持续时间");
                        return;
                    }

                    if (!IntensityStageTimeUnitSelect.Text.IsNullOrEmpty())
                    {
                        model.IntensityStageTimeUnit = IntensityStageTimeUnitSelect.Text;
                    }
                    else
                    {
                        Error("请选择强度段时间单位");
                        return;
                    }

                    if (!IntensityStageEstimateWaySelect.Text.IsNullOrEmpty())
                    {
                        model.IntensityStageEstimateWay = FormatEstimate(IntensityStageEstimateWaySelect.Text);
                    }
                    else
                    {
                        Error("请选择强度段判断方式");
                        model.IntensityStageEstimateWay = 0;
                        return;
                    }

                    if (!IntensityStageTimeUnitSelect.Text.IsNullOrEmpty())
                    {
                        model.IntensityStageEstimateValue = Math.Round(double.Parse(IntensityStageEstimateValueInput.Text), 3);
                    }
                    else
                    {
                        Error("请输入强度段判断数值");
                        return;
                    }

                }
                catch (Exception)
                {
                    Error("请输入强度段持续时间");
                    return;
                }
            }

            if (!RigorStagePressureInput.Text.IsNullOrEmpty())
            {
                try
                {
                    model.RigorStagePressure = Math.Round(double.Parse(RigorStagePressureInput.Text), 3);
                    if (!RigorStageTimeInput.Text.IsNullOrEmpty())
                    {
                        model.RigorStageTime = int.Parse(RigorStageTimeInput.Text);
                    }
                    else
                    {
                        Error("请输入严密性持续时间");
                        return;
                    }

                    if (!RigorStageTimeUnitSelect.Text.IsNullOrEmpty())
                    {
                        model.RigorStageTimeUnit = RigorStageTimeUnitSelect.Text;
                    }
                    else
                    {
                        Error("请选择严密性时间单位");
                        return;
                    }

                    if (!RigorStageEstimateWaySelect.Text.IsNullOrEmpty())
                    {
                        model.RigorStageEstimateWay = FormatEstimate(RigorStageEstimateWaySelect.Text);
                    }
                    else
                    {
                        Error("请选择严密性判断方式");
                        model.RigorStageEstimateWay = 0;
                        return;
                    }

                    if (!RigorStageTimeUnitSelect.Text.IsNullOrEmpty())
                    {
                        model.RigorStageEstimateValue = Math.Round(double.Parse(RigorStageEstimateValueInput.Text), 3);
                    }
                    else
                    {
                        Error("请输入严密性判断数值");
                        return;
                    }

                }
                catch (Exception)
                {
                    Error("请输入严密性持续时间");
                    return;
                }
            }

            Response response = Services.AddTemplate(model);
            if (response.Code != 200)
            {
                Error(response.Msg);
                return;
            }
            else
            {
                this.Close();
                if (this.Owner is not null)
                {
                    TemplateManageForm frm = (TemplateManageForm)this.Owner;
                    frm.LoadList();
                }
            }
        }
        #endregion
    }
}
